﻿using System.Web.Mvc;

namespace TSharp.Core.Mvc.Web
{
    /// <summary>
    ///     控制器方法调用器
    ///     <para>2010/9/16</para>
    ///     <para>TANGJINGBO</para>
    ///     <author>tangjingbo</author>
    /// </summary>
    public class TSharpControllerActionInvoker : ControllerActionInvoker
    {
        /// <summary>
        ///     控制器方法调用器
        /// </summary>
        public static readonly TSharpControllerActionInvoker Instance = new TSharpControllerActionInvoker();

        private TSharpControllerActionInvoker()
        {
        }

        /// <summary>
        ///     检索有关操作筛选器的信息。
        /// </summary>
        /// <param name="controllerContext">控制器上下文。</param>
        /// <param name="actionDescriptor">操作描述符。</param>
        /// <returns>有关操作筛选器的信息。</returns>
        protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            FilterInfo baseFilters = base.GetFilters(controllerContext, actionDescriptor);
            //baseFilters.ActionFilters.Add(new CultureActionFilterAttribute());
            //baseFilters.ActionFilters.Add(ExecutionTimingActionFilterAttribute.Instance);
            //   baseFilters.ExceptionFilters.Add(ExceptionFilter.Instance); 
            return baseFilters;
        }

        /// <summary>
        ///     创建操作结果。
        /// </summary>
        /// <param name="controllerContext">控制器上下文。</param>
        /// <param name="actionDescriptor">操作描述符。</param>
        /// <param name="actionReturnValue">操作返回值。</param>
        /// <returns>操作结果对象。</returns>
        protected override ActionResult CreateActionResult(ControllerContext controllerContext,
                                                           ActionDescriptor actionDescriptor, object actionReturnValue)
        {
            /***********原来mvc中实现****************
            if (actionReturnValue == null)
            {
                return new EmptyResult();
            }

            ActionResult actionResult = (actionReturnValue as ActionResult) ??
                new ContentResult { Content = Convert.ToString(actionReturnValue, CultureInfo.InvariantCulture) };
            return actionResult;
            /***********原来mvc中实现****************/

            if (actionReturnValue is ActionResult)
                return actionReturnValue as ActionResult;

            controllerContext.Controller.ViewData.Model = actionReturnValue;
            return new ViewResult
                       {
                           ViewData = controllerContext.Controller.ViewData,
                           TempData = controllerContext.Controller.TempData
                       };
        }
    }
}